const {
	Backend,
	Joi
} = require('dx-router-admin');

module.exports = class  Project extends Backend {
	constructor(ctx) {
		super(ctx);
		this.multiFields = [];
		this.indexWith = ['company','hr'];
		this.listWith = ['company'];
		this.dto = {
			// name: Joi.string().required().error(new Error('名称不能为空'))
		};
	}
	async index(params){
		if(params.company_name){
			const clist = await this.$dx.service.company.findCompanyIds(params);
			params.company_id = ['in',_.pluck(clist,'_id')]
			delete params.company_name;
		}
		if(this.$dx.auth.role.includes('admin')){
			return super.index(params);
		}
		
		
		let {
			page = 1, size = 10, order, sort, selectPage
		} = params;
		for (let key of ['page', 'size', 'order', 'sort', 'selectPage']) {
			Reflect.deleteProperty(params, key);
		}
		page = page - 1;
		page = page < 0 ? 0 : page;
		let entity = this.currentService.whereOr().where({...params,user_id:this.$dx.auth.uid}).where({...params,cooperate:['contain',this.$dx.auth.uid]});
		if (order && typeof order === 'string') {
			entity = entity.order(order, ['asc', 'ASC'].includes(sort) ? 'asc' : 'desc');
		}
		if (this.indexWith.length > 0) {
			entity = entity.with(this.indexWith);
		}
		let list = await entity
			.field(this.fields)
			.limit(size)
			.skip(page * size)
			.select();
		//下拉选择
		let total = await this.currentService.whereOr().where({...params,user_id:this.$dx.auth.uid}).where({...params,cooperate:['contain',this.$dx.auth.uid]}).count();
		return this.$dx.success(this.$dx.$t('operation succeeded'), {
			list,
			total,
		});
	}
	async list(param){
		//param.status = ['in',[0,1]];
		
		const list = await super.list(param);
		const uid = this.$dx.auth.uid;
		if(!this.$dx.auth.role.includes('admin')){
			list.data = list.data.filter(item=>{
				const flag =  item.user_id==uid||(item.cooperate&&item.cooperate.includes(uid));
				return flag;
			})
		}
		return list;
	}
	import(param){
		return this.currentService.importFromJob(param);
	}
	async get(param){
		return await this.currentService.getProject(param);
	}
	getByTalentId(param){
		return this.currentService.getByTalentId(param);
	}
	getByCompany(param){
		return this.currentService.getByCompany(param);
	}
	addProjectTarget(param){
		return this.currentService.addProjectTarget(param);
	}
};